package com.woniu.finance.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.finance.entity.FinanceUser;
import com.woniu.finance.entity.RequestParam;
import com.woniu.finance.mapper.UserMapper;
import com.woniu.finance.service.IUserService;
import com.woniu.util.utils.HttpResult;
import com.woniu.util.utils.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘品水
 * @since 2023-05-05
 */
@Service
public class UserServiceImpl implements IUserService {
    @Autowired(required = false)
    private UserMapper mapper;

    @Override
    public HttpResult getUsersInfoByAccount(String userName) {
        FinanceUser user = mapper.getUsersInfoByUserName(userName);
        if (user != null) {
            return new HttpResult().ok(user);
        } else {
            return new HttpResult().fail();
        }
    }

    @Override
    public HttpResult queryFinanceUserInfo(RequestResult<FinanceUser> request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<FinanceUser> financeUsers = mapper.queryFinanceUserInfo
                (request.getData().getUserName(),
                        request.getData().getUserName(),
                        request.getData().getPhone(),
                        request.getData().getState(),
                        request.getData().getRole().getId());
        PageInfo pageInfo = new PageInfo(financeUsers);
        return new HttpResult().ok(pageInfo);
    }

    @Override
    public HttpResult changeUserInfo(FinanceUser financeUser) {
        mapper.updateById(financeUser);
        mapper.changeUserInfo(financeUser.getId(), financeUser.getRole().getId());
        return new HttpResult().ok();
    }

    @Override
    public HttpResult insertUserInfo(FinanceUser financeUser) {
        mapper.insert(financeUser);
        mapper.insertUserByUandR(financeUser.getId(), financeUser.getRole().getId());
        return new HttpResult().ok();
    }

    @Override
    public HttpResult getEnterpriseIdByUserName(String userName) {
        String enterpriseId = "";
        try {
            enterpriseId = mapper.getEnterpriseIdByUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail();
        }
        return new HttpResult().ok(enterpriseId);
    }

    @Override
    public HttpResult getUserIdByUserName(String userName) {
        Integer userId = null;
        try {
            userId = mapper.getUserIdByUserName(userName);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult().fail();
        }
        return new HttpResult().ok(userId);
    }

    //通过融资企业id查询用户信息
    @Override
    public HttpResult findAllUserByEnterpriseId(RequestParam<FinanceUser> param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FinanceUser> allUserByEnterpriseId = mapper.findAllUserByEnterpriseId(param.getData().getEnterpriseId());
        PageInfo pageInfo = new PageInfo(allUserByEnterpriseId);
        return new HttpResult<>().ok(pageInfo);
    }
}
